import { contextBridge } from 'electron';
import { electronAPI } from '@electron-toolkit/preload';
import * as userApi from '../main/userApi';//用户api交互js
// Custom APIs for renderer
const api = { ...userApi };

// 处理服务器自启的功能
api.getItemList().then(arr => {
  if(!Array.isArray(arr)) return;
  for (let item of arr) {
    if (!item.autorun) continue;//没有开启自启直接跳过
    // 启动开启了自启的服务器
    //调用启动服务器
    electronAPI.ipcRenderer.invoke('createProcess', item.uuid, item.start, item.path).catch(err => { });
  }
});

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
  } catch (error) {
    console.error(error)
  }
} else {
  window.electron = electronAPI
  window.api = api
}


